package com.itheima.tlais_web_management.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//@Aspect
@Component
public class MyAspect {

    @Pointcut("execution(* com.itheima.tlais_web_management.service.*.*(..))")
    public void pt(){}

    // 前置通知
    @Before("pt()")
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("Before...");
    }

    // 后置通知
    @After("pt()")
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("After...");
    }

    // 返回通知
    @AfterReturning(pointcut = "pt()", returning = "result")
    public void afterReturningAdvice(JoinPoint joinPoint, Object result) {
        System.out.println("After-returning..." + result);
    }

    // 异常通知
    @AfterThrowing(pointcut = "pt()", throwing = "exception")
    public void afterThrowingAdvice(JoinPoint joinPoint, Exception exception) {
        System.out.println("After-throwing..." + exception);
    }

    /**
     *
     * 1、环绕通知：需要考虑 调用目标方法
     *
     * 2、环绕通知：需要考虑 返回值（ Object ）
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */

    // 环绕通知
    @Around("pt()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Around...Before...");
        Object result = joinPoint.proceed();
        System.out.println("Around...After...");
        return result;
    }
}